<template>
  <div class="app-container" v-loading="loading">
    <el-form :inline="true" :model="queryParams">
      <div style="display: flex; flex-wrap: wrap">
        <el-col :span="4">
          <el-form-item>
            <el-input
              v-model="queryParams.receiptCode"
              placeholder="入库单号"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-select
              clearable
              placeholder="状态"
              style="width: 100%"
              v-model="queryParams.status"
            >
              <template v-for="(item, index) in receipt_status" :key="index">
                <el-option :label="item.label" :value="item.value"></el-option>
              </template>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-input
              v-model="queryParams.purchaseCode"
              placeholder="关联订单号"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-select
              clearable
              placeholder="订单类型"
              style="width: 100%"
              v-model="queryParams.receiptType"
            >
              <template v-for="(item, index) in receipt_type" :key="index">
                <el-option :label="item.label" :value="item.value"></el-option>
              </template>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-select
              v-model="queryParams.warehouseId"
              style="width: 100%"
              clearable
              placeholder="入库仓库"
            >
              <el-option
                v-for="item in warehouseList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-input
              v-model="queryParams.vendorName"
              placeholder="供应商"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-input
              v-model="queryParams.createBy"
              placeholder="创建人"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-date-picker
              style="width: 100%"
              v-model="timeData"
              start-placeholder="收货开始时间"
              end-placeholder="结束时间"
              range-separator="-"
              type="datetimerange"
              value-format="YYYY-MM-DD HH:mm:ss"
              unlink-panels
            />
          </el-form-item>
        </el-col>

        <el-col :span="4">
          <el-form-item>
            <el-button icon="Search" type="primary" @click="clickSearch"
              >查询</el-button
            >
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </div>
    </el-form>
    <div>
      <el-button type="primary" @click="clickPrint">打印商品库标签</el-button>
    </div>
    <vxe-table
      ref="tableRef"
      border
      show-overflow
      align="center"
      :data="tableData"
      :height="tableHeight"
      :row-config="{ isCurrent: true, isHover: true }"
      style="margin-top: 10px"
      :checkbox-config="{ trigger: 'row' }"
    >
      <vxe-column type="checkbox" width="60"></vxe-column>
      <vxe-column type="seq" width="80" title="序号" tree-node></vxe-column>
      <vxe-column field="receiptCode" width="200" title="入库单号"></vxe-column>
      <vxe-column field="status" width="120" title="状态">
        <template v-slot="{ row }">
          {{ returnDictLabel(receipt_status, row.status) }}
        </template>
      </vxe-column>
      <vxe-column field="totalNum" width="200" title="总收货数量"></vxe-column>
      <vxe-column field="receiptNum" width="200" title="实收数量"></vxe-column>
      <vxe-column
        field="qcPassNum"
        width="200"
        title="质检正品数量"
      ></vxe-column>
      <vxe-column field="putawayPercent" width="200" title="上架指标">
        <template v-slot="{ row }"> {{ row.putawayPercent }}% </template>
      </vxe-column>
      <vxe-column
        field="purchaseCode"
        width="200"
        title="关联订单号"
      ></vxe-column>
      <vxe-column field="receiptType" width="150" title="订单类型">
        <template v-slot="{ row }">
          {{ returnDictLabel(receipt_type, row.receiptType) }}
        </template>
      </vxe-column>
      <vxe-column field="warehouseName" width="200" title="仓库"></vxe-column>
      <vxe-column
        field="vendorCode"
        width="200"
        title="供应商编码"
      ></vxe-column>
      <vxe-column field="vendorName" width="200" title="供应商"></vxe-column>
      <vxe-column field="remark" width="200" title="备注"></vxe-column>
      <vxe-column field="receiptByName" width="200" title="收货人"></vxe-column>
      <vxe-column field="receiptTime" width="200" title="收货时间"></vxe-column>
      <vxe-column fixed="right" width="200" title="操作">
        <template #default="{ row }">
          <el-button type="text" style="color: green" @click="clickView(row)">
            收货明细
          </el-button>
        </template>
      </vxe-column>
    </vxe-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <detailDialog
      :isShowDialog="isShowDialog"
      v-if="isShowDialog"
      :receipt-id="receiptId"
      @cancel="handleClose"
    ></detailDialog>

    <!--    商品条码打印-->
    <GoodsBarcodePrint
      :isShowDialog="isShowGoodsPrint"
      v-if="isShowGoodsPrint"
      :detailData="printRows"
      @cancel="closePrint"
    ></GoodsBarcodePrint>
  </div>
</template>

<script setup>
import { nextTick, onMounted, ref } from "vue";
import { receiptRecordList, warehouseGetFilter } from "@/api/scApi";
import { flattenData } from "@/utils/shuangcheng";
import DetailDialog from "@/views/inStorage/receivingRecords/detailDialog.vue";
import GoodsBarcodePrint from "@/views/inStorage/receivingRecords/goodsBarcodePrint.vue";

const { proxy } = getCurrentInstance();
const { receipt_status, receipt_type } = proxy.useDict(
  "receipt_status",
  "receipt_type"
);

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  startTime: "",
  endTime: "",
});
let total = ref(1);
let isView = ref(false);
let timeData = ref([]);
let loading = ref(false);
let isShowGoodsPrint = ref(false);
let printRows = ref([]);
const clickSearch = () => {
  getList();
};

const resetQuery = () => {
  timeData.value = [];
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    startTime: "",
    endTime: "",
  };
  getList();
};

function getList() {
  loading.value = true;
  queryParams.value.startTime = timeData.value[0];
  queryParams.value.endTime = timeData.value[1];

  receiptRecordList({
    pageNum: queryParams.value.pageNum,
    pageSize: queryParams.value.pageSize,
    param: queryParams.value,
  })
    .then((res) => {
      loading.value = false;
      tableData.value = flattenData(res.data);
      total.value = res.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

onMounted(() => {
  getWarehouseFilter();
  getList();
});

let tableHeight = ref(0);
nextTick(() => {
  tableHeight.value = window.innerHeight - 350;
});
window.onresize = () => {
  tableHeight.value = document.body.scrollHeight - 350;
};

let tableData = ref([]);

let isShowDialog = ref(false);

const receiptId = ref("");
function clickView({ receiptId: id }) {
  receiptId.value = id;
  isShowDialog.value = true;
}

function handleClose(isReload) {
  isShowDialog.value = false;
  isView.value = false;
  detailData.value = {};
  if (isReload) {
    getList();
  }
}

function clickPrint() {
  const $table = proxy.$refs.tableRef;
  if ($table) {
    const selectRecords = $table.getCheckboxRecords();
    if (selectRecords.length === 0) {
      proxy.$modal.msgWarning("请选择要打印的数据!");
    } else {
      printRows.value = selectRecords.filter((item) => !item.parentId);
      isShowGoodsPrint.value = true;
    }
  }
}

function closePrint() {
  printRows.value = {};
  isShowGoodsPrint.value = false;
  getList();
}

//获取仓库
let warehouseList = ref([]);

function getWarehouseFilter() {
  warehouseGetFilter({
    status: 0,
  }).then((res) => {
    if (res.code === 200) {
      warehouseList.value = res.data.map((item) => {
        item.value = item.warehouseId;
        item.label = item.warehouseName;
        return item;
      });
    }
  });
}
</script>

<style scoped lang="scss"></style>
